package com.ctg.payroll2.service.social.impl;

import java.sql.SQLException;
import java.util.List;

import com.ctg.payroll2.bean.filter.social.CsEmplsocialFilter;
import com.ctg.payroll2.bean.filter.social.PbEmployeeFilter;
import com.ctg.payroll2.bean.po.social.CsEmplsocial;
import com.ctg.payroll2.dao.social.EmployeeSocialRecordDao;
import com.ctg.payroll2.service.BaseService;
import com.ctg.payroll2.service.social.CsEmplsocialService;
import com.ctg.platform.util.Page;
/**
 * 员工社保档案管理Service 实现
 * @author 鲁力 2009-9-7 下午03:19:50
 *
 */
@SuppressWarnings("unchecked")
public class CsEmplsocialServiceImpl extends BaseService implements CsEmplsocialService {
	private EmployeeSocialRecordDao dao;
	/**
	 * @param dao 设置dao
	 */
	public void setDao(EmployeeSocialRecordDao dao) {
		this.dao = dao;
	}

	public Page queryCsEmplsocial(CsEmplsocialFilter filter) {
		return dao.queryCsEmplsocial(filter);
	}
	
	public Page queryPbSocialItemsResult(CsEmplsocialFilter filter){
		return dao.queryPbSocialItemsResult(filter);
	}
	
	public Page queryCsEmployee(PbEmployeeFilter filter){
		return dao.queryCsEmployee(filter);
	}

	public Page queryRecomSocialTemplate(CsEmplsocialFilter filter) {
		return dao.queryRecomSocialTemplate(filter);
	}
	
	public List queryRecomSocialTemplateDetail(CsEmplsocialFilter filter){
		return dao.queryRecomSocialTemplateDetail(filter);
	}

	public Page queryCsSocialTemplate(CsEmplsocialFilter filter) {
		return dao.queryCsSocialTemplate(filter);
	}
	public List queryCsSocialTemplateDetail(CsEmplsocialFilter filter){
		return dao.queryCsSocialTemplateDetail(filter);
	}
	
	/**
	 * 员工社保档案详细信息翻页查询
	 * @param filter
	 * @return
	 */
	public Page querycsEmplsocialDetails(CsEmplsocialFilter filter){
		return dao.querycsEmplsocialDetails(filter);
	}

	public void insertCsEmplsocial(List<CsEmplsocial> csEmplsocialList) throws SQLException {
		if(csEmplsocialList==null)return;
		for (CsEmplsocial emplsocial : csEmplsocialList) {
			dao.insertCsEmplsocial(emplsocial);
		}
	}
	
	public void updateCsEmplsocial(List<CsEmplsocial> addsList,
			List<CsEmplsocial> modifList, List<CsEmplsocial> delsList) throws SQLException {
		if (addsList != null && addsList.size()>0) {
			for (CsEmplsocial emplsocial : addsList) {
				dao.insertCsEmplsocial(emplsocial);
			}
		}
		if (modifList != null && modifList.size()>0) {
			for (CsEmplsocial emplsocial : modifList) {
				if(checkisNeedToDemolish(emplsocial)){
					dao.demolishCsEmpSocial(emplsocial);
				}else{//直接update
					dao.updateCsEmpSocial(emplsocial);
				}
			}
		}
		if (delsList != null && delsList.size()>0) {
			StringBuffer sb = new StringBuffer();
			for (CsEmplsocial emplsocial : delsList) {
				sb.append(emplsocial.getEmployee_ss_id()).append(",");
			}
			String ids = sb.substring(0, sb.length() - 1);
			dao.fakeDeleteCsEmplsocial(ids);
		}
	}
	
	public void batchAdjustSocial(List<CsEmplsocial> list) throws SQLException, Exception {
		if (list!=null&&list.size()>0) {
			CsEmplsocialFilter filter = new CsEmplsocialFilter();
			for (CsEmplsocial emplsocial : list) {
				filter.setEmployee_id(emplsocial.getEmployee_id().toString());
				filter.setSocialItemId(emplsocial.getSocial_security_id());
				CsEmplsocial emplsocial_temp = dao.retrieveEmployeeByFilter(filter);
				emplsocial.setEmployee_ss_id(emplsocial_temp.getEmployee_ss_id());
				dao.demolishCsEmpSocial(emplsocial);
			}
		}
	}

	private Boolean checkisNeedToDemolish(CsEmplsocial emplsocial) throws SQLException {
		int i = dao.checkisNeedToDemolish(emplsocial);
		if (i == -1) {
			throw new SQLException("emplsocial has no result with the id is "+emplsocial.getEmployee_ss_id());
		}else if(i == 0){
			return false;
		}
		else if(i == 1){
			return true;
		}
		return null;
	}
	
	public List getEmployeeBySocialItem(CsEmplsocialFilter filter) throws SQLException{
		return dao.getEmployeeBySocialItem(filter);
	}
	
	public String checkEmpSSocialItemIsCaled(CsEmplsocial csEmplsocial) throws SQLException{
		return dao.checkEmpSSocialItemIsCaled(csEmplsocial);
	}
	public int bjrjg(CsEmplsocial csEmplsocial) throws SQLException{
		return dao.bjrjg(csEmplsocial);
	}
}
